﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	public partial class VoteResult
	{
		private const string SelectedIndexSeparator = ",";
		public int[] SelectedIndexs
		{
			get
			{
				return Hxsoft.StringExtensions.Decompound<int>(this.SelectedItems, SelectedIndexSeparator, StringSplitOptions.RemoveEmptyEntries);
			}
			set
			{
				this.SelectedItems = Hxsoft.StringExtensions.Compound<int>(value, SelectedIndexSeparator, StringSplitOptions.RemoveEmptyEntries);
			}
		}
	}




	/// <summary>
	/// VoteResults 类。
	/// </summary>
	public static partial class VoteResults
	{
		public const string INFO_TYPE = "VoteResult";

		#region GetItem
		/// <summary>
		/// 根据标识获取指定的调查结果项。
		/// </summary>
		/// <param name="o">调查结果项集合。</param>
		/// <param name="resultId">获取的调查结果项标识 ResultId。</param>
		/// <returns>获取的信息项。</returns>
		public static VoteResult GetItem(this System.Data.Linq.Table<VoteResult> o, int resultId)
		{
			return o.SingleOrDefault(p => p.ResultId == resultId);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的调查结果项是否已经存在。
		/// </summary>
		/// <param name="o">调查结果项集合。</param>
		/// <param name="resultId">验证的调查结果项标识 ResultId。</param>
		/// <returns>布尔值表示指定的调查结果项是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<VoteResult> o, int resultId)
		{
			return o.Any(p => p.ResultId == resultId);
		}
		#endregion
	}



	/// <summary>
	/// VoteResults 类操作部分。
	/// </summary>
	public static partial class VoteResults
	{
		#region SetAward
		private static readonly HashSet<string> _settedAwardColumns = typeof(VoteResult).GetEntityColumnNames(new string[] {
				"IsAward",
				"AwardContent"
			}, true);

		/// <summary>
		/// 设置调查项的获奖信息。
		/// </summary>
		/// <param name="resultId">调查项标识。</param>
		/// <param name="isAward">是否获奖。</param>
		/// <param name="awardContent">获奖内容。</param>
		/// <returns>设置的调查项。</returns>
		public static VoteResult SetAward(VoteResult settedResult)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			VoteResult result = dc.VoteResults.GetItem(settedResult.ResultId);
			if (result == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetSetAwardRight(dc, result, true);	// 权限验证

			result.FillByEntity(settedResult, _settedAwardColumns);

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 记录日志
			Logs.Insert(dc, null, null, INFO_TYPE, result.ResultId.ToString(), "SetAward", result.AwardContent);

			// 返回只读数据
			return settedResult.FillByEntity(result);
		}
		#endregion

		#region Delete
		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, VoteResult voteResult)
		{
			dc.VoteResults.DeleteOnSubmit(voteResult);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, Vote parent)
		{
			IEnumerable<VoteResult> e = parent.VoteResults.AsEnumerable();
			foreach (VoteResult i in e)
				Delete(dc, i);
		}
		#endregion
		#endregion
	}



	/// <summary>
	/// VoteResults 类对应操作权限部分。
	/// </summary>
	public static partial class VoteResults
	{
		#region GetSettingRight
		/// <summary>
		/// 获取当前用户是否有修改调查结果的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="dic">修改的调查结果对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有修改调查结果的权限。</returns>
		public static bool GetSettingRight(GenericModuleDataContext dc, VoteResult result, bool triggerException)
		{
			Vote vote = dc.Votes.GetItem(result.VoteId);
			return Votes.GetSettingRight(dc, vote, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有修改调查结果的权限。
		/// </summary>
		/// <param name="dic">修改的调查结果对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有修改调查结果的权限。</returns>
		public static bool GetSettingRight(VoteResult result, bool triggerException)
		{
			return GetSettingRight(new GenericModuleDataContext(), result, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有修改调查结果的权限。
		/// </summary>
		/// <param name="dic">修改的调查结果对象。</param>
		/// <returns>布尔值表示当前用户是否有修改调查结果的权限。</returns>
		public static bool GetSettingRight(VoteResult result)
		{
			return GetSettingRight(result, false);
		}
		#endregion

		#region GetSetAwardRight
		/// <summary>
		/// 获取当前用户是否有修改调查结果的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="dic">修改的调查结果对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有修改调查结果的权限。</returns>
		public static bool GetSetAwardRight(GenericModuleDataContext dc, VoteResult result, bool triggerException)
		{
			return GetSettingRight(dc, result, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有修改调查结果的权限。
		/// </summary>
		/// <param name="dic">修改的调查结果对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有修改调查结果的权限。</returns>
		public static bool GetSetAwardRight(VoteResult result, bool triggerException)
		{
			return GetSetAwardRight(new GenericModuleDataContext(), result, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有修改调查结果的权限。
		/// </summary>
		/// <param name="dic">修改的调查结果对象。</param>
		/// <returns>布尔值表示当前用户是否有修改调查结果的权限。</returns>
		public static bool GetSetAwardRight(VoteResult result)
		{
			return GetSetAwardRight(result, false);
		}
		#endregion
	}



	/// <summary>
	/// VoteResults 类前台显示部分。
	/// </summary>
	public static partial class VoteResults
	{
		/// <summary>
		/// 基础列表。
		/// </summary>
		public struct VoteResultForList
		{
			public int ResultId { get; set; }
			public int VoteId { get; set; }
			public string Ip { get; set; }
			public string SelectedItems { get; set; }
			public string UserDefinedContent { get; set; }
			public Guid? UserId { get; set; }
			public DateTime VoteDate { get; set; }
			public string UserName { get; set; }
			public string Name { get; set; }
			public string Telephone { get; set; }
			public bool IsAward { get; set; }
			public string AwardContent { get; set; }
		}


		/// <summary>
		/// 获取基本列表查询。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="voteId">调查标识。</param>
		/// <param name="hasUserDefinedContent">是否有用户定义的内容。</param>
		/// <param name="keyword">检索的关键字。</param>
		/// <returns>基本列表查询。</returns>
		private static IQueryable<VoteResultForList> GetListQuery(GenericModuleDataContext dc, int? voteId, bool? hasUserDefinedContent, string keyword)
		{
			IQueryable<VoteResultForList> q;
			q = from p in dc.VoteResults
				join p2 in dc.Users on p.UserId equals p2.UserId into p3
				from p4 in p3.DefaultIfEmpty()
				orderby p.VoteDate descending, p.ResultId descending
				select new VoteResultForList { ResultId = p.ResultId, VoteId = p.VoteId, Ip = p.Ip, SelectedItems = p.SelectedItems, UserDefinedContent = p.UserDefinedContent, UserId = p.UserId, VoteDate = p.VoteDate, UserName = p4.UserName, Name = p.Name, Telephone = p.Telephone, IsAward = p.IsAward, AwardContent = p.AwardContent };

			if (voteId.HasValue)
			{
				q = q.Where(p => p.VoteId == voteId.Value);
			}

			if (hasUserDefinedContent.HasValue)
			{
				if (hasUserDefinedContent.Value)
					q = q.Where(p => p.UserDefinedContent.Length > 0);
				else
					q = q.Where(p => p.UserDefinedContent == null || p.UserDefinedContent.Length == 0);
			}

			if (!string.IsNullOrEmpty(keyword))
				q = q.Where(p => p.UserDefinedContent.Contains(keyword));

			return q;
		}

		/// <summary>
		/// 获取分页基本列表。
		/// </summary>
		/// <param name="voteId">调查标识。</param>
		/// <param name="hasUserDefinedContent">是否有用户定义的内容。</param>
		/// <param name="keyword">检索的关键字。</param>
		/// <param name="pageSize">页尺寸。</param>
		/// <param name="currentPage">当前页码。</param>
		/// <returns>分页基本列表。</returns>
		public static PaginationInfo<List<VoteResultForList>> GetPaginationList(int? voteId, bool? hasUserDefinedContent, string keyword, int pageSize, int currentPage)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			IQueryable<VoteResultForList> q = GetListQuery(dc, voteId, hasUserDefinedContent, keyword);

			PaginationInfo<List<VoteResultForList>> pageInfo = new PaginationInfo<List<VoteResultForList>>();
			pageInfo.PageSize = pageSize;
			pageInfo.CurrentPage = currentPage;
			pageInfo.RecordCount = q.Count();
			pageInfo.Records = q.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
			return pageInfo;
		}

		/// <summary>
		/// 获取基本列表。
		/// </summary>
		/// <param name="voteId">调查标识。</param>
		/// <param name="hasUserDefinedContent">是否有用户定义的内容。</param>
		/// <param name="keyword">检索的关键字。</param>
		/// <param name="loadCount">读取的数量。</param>
		/// <returns>基本列表。</returns>
		public static List<VoteResultForList> GetList(int? voteId, bool? hasUserDefinedContent, string keyword, int? loadCount)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			IQueryable<VoteResultForList> q = GetListQuery(dc, voteId, hasUserDefinedContent, keyword);

			if (loadCount.HasValue)
				return q.Take(loadCount.Value).ToList();
			return q.ToList();
		}
	}
}